<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
%>
<html>
<head>
    <title>楼房信息</title>
</head>
<body>
<jsp:include page="../common/css_js.jsp"/>
<div class="page-container">
    <div class="mt-3  mb-3">
        <h3>楼房信息</h3>
    </div>
    <table class="table" id="myTable">
        <thead>
        <tr>
            <th>序号</th>
            <th>宿舍名</th>
            <th>宿舍类别</th>
            <th>房间数</th>
            <th>楼层数</th>
            <th>可住人数</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${BUILDING.list}" var="one" varStatus="sta">
            <tr id="b_${one.buildId}">
                <td>${one.buildId}</td>
                <td>${one.buildName}</td>
                <td>${one.buildSex}</td>
                <td>${one.roomAmount}</td>
                <td>${one.floorAmount}</td>
                <td>${one.personAmount}</td>
                <td>
                    <a class="btn btn-success" href="javascript:" onclick="update(${one.buildId})">修改</a>
                    <a class="btn btn-danger btn-sm" href="javascript:;" onclick="del(${one.buildId})">删除</a>
                </td>
            </tr>
        </c:forEach>
        </tbody>
        <!--分页功能-->
        <script>
            var pageSize = ${BUILDING.pageSize};
            var pageNumber = ${BUILDING.currentPage};
            var totalPage = ${BUILDING.totalPage};

            function firstPage() {
                if (pageNumber != 1) {
                    pageNumber = 1;
                    gotoPage();
                }
            }

            function prevPage() {
                if (pageNumber > 1) {
                    pageNumber--;
                    gotoPage();
                }
            }
            function nextPage() {
                if (pageNumber < totalPage) {
                    pageNumber++;
                    gotoPage();
                }
            }
            function lastPage() {
                if (pageNumber != totalPage) {
                    pageNumber = totalPage;
                    gotoPage();
                }
            }
            function gotoPage() {
                window.location.href = "${pageContext.request.contextPath}/building/getBuilding?pageSize=" +
                    pageSize + "&pageNumber=" + pageNumber;
            }
        </script>
    </table>
    <div>
        <button onclick="firstPage()">首页</button>
        <button onclick="prevPage()">上一页</button>
        <button onclick="nextPage()">下一页</button>
        <button onclick="lastPage()">尾页</button>
        <span>当前第${BUILDING.currentPage}页，共${BUILDING.totalPage}页，共${BUILDING.totalSize}条记录</span>
    </div>
</div>

<!-- 修改用的模态框 -->
<div class="modal fade" id="updateModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">修改楼房信息</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form id="editForm" method="post" onsubmit="return false;">
                    <div class="form-group">
                        <label>序号</label>
                        <input type="text" name="buildId" id="buildId1" class="form-control" readonly="true">
                    </div>
                    <div class="form-group">
                        <label>宿舍名</label>
                        <input type="text" name="buildName" id="buildName1" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>宿舍类别</label>
                        <input type="text" name="buildSex" id="buildSex1" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>房间数</label>
                        <input type="text" name="roomAmount" id="roomAmount1" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>楼层数</label>
                        <input type="text" name="floorAmount" id="floorAmount1" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>可住</label>
                        <input type="text" name="personAmount" id="personAmount1" class="form-control">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="update_do()">提交</button>
            </div>
        </div>
    </div>
</div>
<script>
    // 修改用的
    function update(buildId) {
        $("#updateModal").modal("show");

        //法一：把表格的第id行的记录拿出来，反填到模态框中
        $("#buildId1").val(buildId);
        $("#buildName1").val($("#b_" + buildId + " .buildName").text());
        $("#buildSex1").val($("#b_" + buildId + " .buildSex").text());
        $("#roomAmount1").val($("#b_" + buildId + " .roomAmount").text());
        $("#floorAmount1").val($("#b_" + buildId + " .floorAmount").text());
        $("#personAmount1").val($("#b_" + buildId + " .personAmount").text());
    }

    function update_do() {
        $.ajax({
            url:"updateBuilding",
            method:"post",
            data:{
                buildId:$("#buildId1").val(),
                buildName:$("#buildName1").val(),
                buildSex:$("#buildSex1").val(),
                roomAmount:$("#roomAmount1").val(),
                floorAmount:$("#floorAmount1").val(),
                personAmount:$("#personAmount1").val(),
            },
            success:function (data) {
                console.log(data);
                if('ok'==data.msg) {
                    alert("修改成功");
                    location.reload();
                }
                else alert("修改失败");
            }
        });
    }
</script>



<script>
    function del(buildId) {
        if (confirm("你真的要删除吗？")) {
            location.href = "delBuilding?buildId=" + buildId;

        }
    }
</script>

<link href="<%=path%>/static/jquery/jquery.dataTables.min.css" rel="stylesheet">
<script src="<%=path%>/static/jquery/jquery.dataTables.min.js"></script>
<script>
    function change() {
        $('#myTable').DataTable();
    }
</script>
</body>
</html>
